package com.haofengsoft.platform.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by weiwei on 15/3/25.
 */
public class DateUtil {
    private static String date_time_format = "yyyy-MM-dd HH:mm:ss";
    private static String date_time_without_sec_format = "yyyy-MM-dd HH:mm";
    private static String date_format = "yyyy-MM-dd";

    public static String gainCurrYear() {
        Calendar c = Calendar.getInstance();
        return String.valueOf(c.get(Calendar.YEAR));
    }

    public static String gainCurrMonth() {
        Calendar c = Calendar.getInstance();
        return String.valueOf(c.get(Calendar.MONTH) + 1);
    }

    /**
     * 使用预设格式将字符串转为Date 返回结果2013-01-24 10:07
     */
    public static String getDateTimeWithoutSecondNow() {
        SimpleDateFormat df = new SimpleDateFormat(date_time_without_sec_format);
        return df.format(new Date());
    }

    public static String getDateNow() {
        SimpleDateFormat df = new SimpleDateFormat(date_format);
        return df.format(new Date());
    }

    // 返回结果2013-01-24 10:07:09
    public static String getDateTimeNow() {
        SimpleDateFormat df = new SimpleDateFormat(date_time_format);
        return df.format(new Date());
    }

    // 返回结果2013-01-24 10:07:09
    public static String getDateTime(long datetime) {
        Date cur_date = new Date(datetime);
        SimpleDateFormat df = new SimpleDateFormat(date_time_format);
        return df.format(cur_date);
    }

    // yyyy-MM-dd
    public static String getDateNoTime(long datetime) {
        Date cur_date = new Date(datetime);
        SimpleDateFormat df = new SimpleDateFormat(date_format);
        return df.format(cur_date);
    }

    // 将时间戳转为字符串
    public static String getStrTime(String cc_time) {
        String re_StrTime = null;
        SimpleDateFormat sdf = new SimpleDateFormat(date_format);
        long lcc_time = Long.valueOf(cc_time);
        re_StrTime = sdf.format(new Date(lcc_time * 1000L));

        return re_StrTime;

    }

    /**
     * 获得指定日期的前一天
     */
    public static String getSpecifiedDayBefore(String specifiedDay) {
        Calendar c = Calendar.getInstance();
        Date date = dateParse(specifiedDay);
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day - 1);
        String dayBefore = new SimpleDateFormat(date_format)
                .format(c.getTime());
        return dayBefore;
    }

    public static long getDateTime(String datetime) {
        Date date = parse(datetime, date_time_format);
        return date.getTime();
    }

    /**
     * 获得指定日期的后一天
     */
    public static String getSpecifiedDayAfter(String specifiedDay) {
        Calendar c = Calendar.getInstance();
        Date date = dateParse(specifiedDay);
        c.setTime(date);
        int day = c.get(Calendar.DATE);
        c.set(Calendar.DATE, day + 1);
        String dayAfter = new SimpleDateFormat(date_format).format(c.getTime());
        return dayAfter;
    }

    /**
     * 使用预设格式将字符串转为Date 参数值 dateParse("2012-01-02") 返回结果 Mon Jan 02 00:00:00 CST
     * 2012
     */
    public static Date dateParse(String strDate) {
        if (strDate == null) {
            return new Date();
        }
        return parse(strDate, date_format);
    }

    public static Date dateParseNoTime(String strDate) {
        if (strDate == null) {
            return new Date();
        }
        return parse(strDate, date_format);
    }

    /**
     * 使用参数Format将字符串转为Date
     */
    public static Date parse(String strDate, String user_format) {
        SimpleDateFormat df = new SimpleDateFormat(user_format);
        try {
            return df.parse(strDate);
        } catch (ParseException e) {
            return null;
        }
    }

    public static int compareDiffYearNumber(String initialDate,
                                            String resultDate) {
        Date initDate = dateParseNoTime(initialDate);
        Date resuDate = dateParseNoTime(resultDate);
        int number = (int) (initDate.getTime() - resuDate.getTime())
                / (1000 * 60 * 60 * 24);
        int count = number / 365;
        return count;
    }

    // 比较日期相差的天数 2014-10-18 2014-10-22 相差 4 天
    // 比较日期相差的天数 2014-10-18 2014-10-16 相差 364天
    // 比较日期相差的天数 2014-10-18 2013-10-16 相差 364天
    // 比较日期相差的天数 2014-10-18 2013-10-22 相差 4天
    public static int compareDiffDateNumber(String initialDate,
                                            String resultDate) {
        try {
            // (int) ((anotherDate.getTime() - today.getTime()) / (1000 * 60 *
            // 60 * 24)))
            Date initDate = dateParseNoTime(initialDate);
            Date resuDate = dateParseNoTime(resultDate);
            int number = (int) ((initDate.getTime() - resuDate.getTime()) / (1000 * 60 * 60 * 24));
            return Math.abs(number);
        } catch (Exception e) {
            return 0;
        }
    }

    // 根据出生年月日获取月份
    public static String getMonthDay(String birthday) {
        String month = "";
        try {
            month = birthday.substring(5, birthday.length());
            return month;
        } catch (Exception ex) {
        }
        return month;
    }

    public static String getWeekOfDate(String strDate) {
        try {
            Date date = parse(strDate, date_format);
            String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五",
                    "星期六"};
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
            if (w < 0) {
                w = 0;
            }
            String dateStr = strDate + "   " + weekDays[w];
            return dateStr;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    // 格式 2015-05-30 18:05:06 获取 月份
    public static String getYearByDateTime(String datetime) {
        SimpleDateFormat sdf = new SimpleDateFormat(date_time_format);
        try {
            Date date = sdf.parse(datetime);
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            return String.valueOf(c.get(Calendar.YEAR));
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    // 格式 2015-05-30 18:05:06 获取 年份
    public static String getMonthByDateTime(String datetime) {
        SimpleDateFormat sdf = new SimpleDateFormat(date_time_format);
        try {
            Date date = sdf.parse(datetime);
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            return String.valueOf(c.get(Calendar.MONTH) + 1);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    //格式  2015-05-30 18:05:06 获取 2015-05-30
    public static String getDateByStringDateTime(String datetime) {
        SimpleDateFormat sdf = new SimpleDateFormat(date_format);
        try {
            Date date = sdf.parse(datetime);

            return sdf.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
}
